#! /usr/bin/env python
#coding=utf-8
#考察点(1)range和xrang(2)sort和sorted
#range与xrange的区别，range返回list，xrange调用一次返回next
#但python 3.0的range相当于2.6的xrange
#参看http://apps.hi.baidu.com/share/detail/2487811
#更多排序 参看http://apps.hi.baidu.com/share/detail/19550749
n = int(input());
a = [input().split() for i in range(n)];
b = {'rat' : 0, 'woman' : 1, 'child' : 1, 'man' : 2, 'captain' : 3};
a.sort(key = lambda x: b[x[1]], reverse = False);
for i in a:
    print(i[0]);